渗透测试之端口转发与代理 常用端口转发方法

您所在的位置:网站首页 netsh advfirewall端口 渗透测试之端口转发与代理 常用端口转发方法

渗透测试之端口转发与代理 常用端口转发方法

2023-04-15 16:36| 来源: 网络整理| 查看: 265

0x00 前言

在渗透测试中,经常会使用到端口转发和代理。

端口转发是转发一个网络端口从一个网络节点到另一个网络节点的行为。

实际应用中需要考虑两种情况:

Client->Transit server->Server:Client能够正向连接Transit server。Transit server直接转发即可ClientServer:Client无法正向连接Transit server,但Transit server能够反向连接Client。

如果Client要对Server的多个端口进行扫描(或是多个Server的多个端口),逐个配置转发规则很不现实。

为了提高效率,这里可以使用代理,配置一次规则即可。

本文将对常用方法和工具进行整理总结,分门别类,区分正向和反向连接,区分转发和代理,分析其中的联系和区别,并给出应用场景,以作参考。

0x01 简介

本文将要介绍以下内容:

针对Windows系统和Linux系统(Ubuntu和CentOS)下的方法端口转发——正向连接的方法端口转发——反向连接的方法正向代理的方法反向代理的方法以上方法的应用场景

注:

Go语言支持跨平台编译,所以本文更侧重于介绍Go语言实现的工具

0x02 端口转发——正向连接0、应用场景1.流量转发的跳板

用于隐藏真实的服务器地址

CIA Hive Beacon Infrastructure中端口转发使用的是Linux的iptables

如下图中的(2)

注:

中转服务器的搭建可参考之前的文章

《CIA Hive Beacon Infrastructure复现1——使用Apache mod_rewrite实现http流量分发》

《CIA Hive Beacon Infrastructure复现2——使用Apache mod_rewrite实现https流量分发》

2.建立通道

连接内网服务器的指定端口

Client能够正向连接到Transit server

1、测试环境

Client: 192.168.111.136

Server: 192.168.111.103

网络连接如下图

使用nc测试网络连接

Server:

[crayon-5bf06608e9423134080522 inline="true" ]nc -lvp 4444 [/crayon]

Client:

[crayon-5bf06608e9429943291836 inline="true" ]nc -vv 192.168.111.103 4444 [/crayon]

Client连接成功,如下图

2、Windows系统下的端口转发方法

Transit server: 192.168.111.132

网络连接如下图

1、使用netsh实现端口转发(需要管理员权限)

(1)添加转发规则

[crayon-5bf06608e942e051192344 inline="true" ]netsh interface portproxy add v4tov4  listenaddress=192.168.111.132 listenport=7777 connectaddress=192.168.111.103  connectport=4444 [/crayon]

(2)添加防火墙入站规则

[crayon-5bf06608e9433071673452 inline="true" ]netsh advfirewall firewall add rule name="transit test" protocol=TCP dir=in localport=7777 action=allow  [/crayon]

注:

默认配置允许出站并阻挡入站通信,所以此处仅需要添加入站规则

测试网络连接:

Server:

[crayon-5bf06608e9438404680833 inline="true" ]nc -lvp 4444 [/crayon]

Client:

[crayon-5bf06608e943c695055938 inline="true" ]nc -vv 192.168.111.132 7777 [/crayon]

Client连接成功

(3)查看端口转发规则

[crayon-5bf06608e9441954582648 inline="true" ]netsh interface portproxy show all [/crayon]

(4)清除端口转发规则

[crayon-5bf06608e9445642362181 inline="true" ]netsh interface portproxy delete v4tov4 listenaddress=192.168.111.132 listenport=7777  [/crayon]

(5)清除防火墙规则

[crayon-5bf06608e944a847333493 inline="true" ]netsh advfirewall firewall delete rule name="transit test" [/crayon]2、使用rinetd实现端口转发

下载地址:

https://boutell.com/rinetd/http/rinetd.zip

仅需要压缩包中的rinetd.exe

(1)为rinetd.exe添加防火墙规则(管理员权限)

[crayon-5bf06608e944e948432317 inline="true" ]netsh advfirewall firewall add rule name="transit test2" dir=in program="c:\test\rinetd.exe" action=allow [/crayon]

(2)编写转发规则

[crayon-5bf06608e9453265275908 inline="true" ]echo 0.0.0.0 7777 192.168.111.103 4444 > conf.txt [/crayon]

(3)启动

[crayon-5bf06608e9457597953497 inline="true" ]rinetd.exe -c c:\test\conf.txt [/crayon]

(4)清除防火墙规则(管理员权限)

[crayon-5bf06608e945c124793017 inline="true" ]netsh advfirewall firewall delete rule name="transit test2" dir=in program="c:\test\rinetd.exe" [/crayon]3、使用HTran实现端口转发

注:

lcx同其功能类似

源码来源于互联网,我在github做了备份,备份地址:

https://raw.githubusercontent.com/3gstudent/test/master/HTran.cpp

(1)为HTran.exe添加防火墙规则(管理员权限)

[crayon-5bf06608e9461915174656 inline="true" ]netsh advfirewall firewall add rule name="transit test3" dir=in program="c:\test\HTran.exe" action=allow [/crayon]

(2)开启转发功能

[crayon-5bf06608e9465732204643 inline="true" ]HTran.exe -tran 7777 192.168.111.103 4444 [/crayon]

(3)清除防火墙规则(管理员权限)

[crayon-5bf06608e9469840760735 inline="true" ]netsh advfirewall firewall delete rule name="transit test3" dir=in program="c:\test\HTran.exe" [/crayon]4、使用EarthWorm实现端口转发

下载地址:

https://github.com/rootkiter/EarthWorm

(1)为ew_for_win_32.exe添加防火墙规则(管理员权限)

[crayon-5bf06608e946e135800521 inline="true" ]netsh advfirewall firewall add rule name="transit test4" dir=in program="c:\test\ew_for_win_32.exe" action=allow [/crayon]

(2)开启转发功能

[crayon-5bf06608e9473540060406 inline="true" ]ew_for_win_32.exe -s lcx_tran -l 7777 -f 192.168.111.103 -g 4444 [/crayon]

(3)清除防火墙规则(管理员权限)

[crayon-5bf06608e9477560841597 inline="true" ]netsh advfirewall firewall delete rule name="transit test4" dir=in program="c:\test\ew_for_win_32.exe" [/crayon]3、Linux系统(Ubuntu)下的常用端口转发方法

Transit server: 192.168.111.102

网络连接如下图

1、使用iptables实现端口转发

(1)开启转发功能

[crayon-5bf06608e947c396214277 inline="true" ]echo 1 >/proc/sys/net/ipv4/ip_forward [/crayon]

注:

该命令立即生效,重启失效

(2)添加转发规则

[crayon-5bf06608e9480258011438 inline="true" ]iptables -t nat -A PREROUTING -p tcp -d 192.168.111.102 --dport 8888 -j DNAT --to-destination 192.168.111.103:4444 iptables -t nat -A POSTROUTING -p tcp -d 192.168.111.103 --dport 4444 -j SNAT --to-source 192.168.111.102 [/crayon]

(3)查看转发规则

[crayon-5bf06608e9485283758485 inline="true" ]iptables -L -t nat --line-number [/crayon]

如下图

测试网络连接:

Server:

[crayon-5bf06608e9489229694814 inline="true" ]nc -lvp 4444 [/crayon]

Client:

[crayon-5bf06608e948e743310537 inline="true" ]nc -vv 192.168.111.102 8888 [/crayon]

Client连接成功

(4)清除规则

[crayon-5bf06608e9492939575389 inline="true" ]iptables -F -t nat [/crayon]

(5)保存规则

[crayon-5bf06608e9497913638006 inline="true" ]iptables-save > /etc/iptables.up.rules [/crayon]

(6)恢复规则

[crayon-5bf06608e949b986704037 inline="true" ]iptables-restore  /etc/rinetd.conf [/crayon]

(3)启动

[crayon-5bf06608e94a9442793251 inline="true" ]./rinetd.exe [/crayon]

(4)结束进程

[crayon-5bf06608e94ad261495602 inline="true" ]pkill -9 rinetd [/crayon]3、使用HTran实现端口转发

Linux版HTran(lcx)的源码参考如下地址:

https://github.com/windworst/LCX

需要使用gcc重新编译

(1)开启转发功能

[crayon-5bf06608e94b2523122052 inline="true" ]./lcx -tran 8888 192.168.111.103 4444 [/crayon]

注:

go语言编写的HTran(lcx),优点是跨平台,支持Windows和Linux

下载地址:

https://github.com/cw1997/NATBypass

4、使用EarthWorm实现端口转发

下载地址:

https://github.com/rootkiter/EarthWorm

未开源

(1)开启转发功能

[crayon-5bf06608e94b6174006303 inline="true" ]./ew_for_linux -s lcx_tran -l 8888 -f 192.168.111.103 -g 4444 [/crayon]4、Linux系统(CentOS)下的常用端口转发方法

Transit server: 192.168.111.105

网络连接如下图

1、使用iptables实现端口转发

(1)开启转发功能

[crayon-5bf06608e94c3284465397 inline="true" ]echo 1 >/proc/sys/net/ipv4/ip_forward [/crayon]

注:

该命令立即生效,系统重启失效

(2)安装iptables

[crayon-5bf06608e94c8174500680 inline="true" ]systemctl stop firewalld   systemctl mask firewalld   yum install iptables-services systemctl enable iptables [/crayon]

(3)添加转发规则

[crayon-5bf06608e94cc229756283 inline="true" ]iptables -t nat -A PREROUTING -p tcp -d 192.168.111.105 --dport 8888 -j DNAT --to-destination 192.168.111.103:4444 iptables -t nat -A POSTROUTING -p tcp -d 192.168.111.103 --dport 4444 -j SNAT --to-source 192.168.111.105 service iptables save service iptables restart service iptables status [/crayon]

(4)查看转发规则

[crayon-5bf06608e94d1773143488 inline="true" ]iptables -L -t nat --line-number [/crayon]

如下图

测试网络连接:

Server:

[crayon-5bf06608e94d5685701834 inline="true" ]nc -lvp 4444 [/crayon]

Client:

[crayon-5bf06608e94da252280789 inline="true" ]nc -vv 192.168.111.105 8888 [/crayon]

Client连接成功

(4)清除规则

[crayon-5bf06608e94de690136957 inline="true" ]iptables -F -t nat [/crayon]2、使用rinetd实现端口转发

同Ubuntu,此处省略

3、使用HTran实现端口转发

同Ubuntu,此处省略

4、使用EarthWorm实现端口转发

同Ubuntu,此处省略

0x03 端口转发——反向连接0、应用场景1.建立通道

连接内网服务器的指定端口

测试环境如下图

已有Transit server权限,想要访问Server的3389端口

Client无法正向连接到Transit server,但Transit server能够反向连接到Client

iptables和rinetd不再适用

1、使用HTran

支持Windows和Linux

Client:

[crayon-5bf06608e94e3330009488 inline="true" ]HTran -listen 1111 2222 [/crayon]

Transit server:

[crayon-5bf06608e94e8927210407 inline="true" ]HTran -slave 1.1.1.1 1111 10.0.0.2 3389 [/crayon]

Client:

[crayon-5bf06608e94ec946703474 inline="true" ]nc -vv 127.0.0.1 2222 [/crayon]2、使用EarthWorm

支持Windows和Linux

Client:

[crayon-5bf06608e94f1807101315 inline="true" ]ew -s lcx_listen -l 2222 -e 1111 [/crayon]

Transit server:

[crayon-5bf06608e94f5334785590 inline="true" ]ew -s lcx_slave -d 1.1.1.1 -e 1111 -f 10.0.0.2 -g 3389 [/crayon]

Client:

[crayon-5bf06608e94fa508166737 inline="true" ]nc -vv 127.0.0.1 2222 [/crayon]0x04 正向代理0、应用场景1.内网扫描

对内网的多个端口进行扫描

Client能够正向连接到Transit server

测试环境如下图

要对Server1、Server2和Server3的端口进行扫描

Socks4代理只支持TCP协议,而Socks5代理支持TCP协议和UDP协议,更加全面,所以本文只介绍实现Socks5代理的方法

1、使用HTran

网上流传HTran2.4支持Socks5代理,但我未找到开源代码,此处作一个标记

2、使用EarthWorm

Transit server:

[crayon-5bf06608e94ff889863102 inline="true" ]ew –s ssocksd –l 8888 [/crayon]

Client使用代理工具连接Transit server的8888端口

3、使用goproxy

go实现的高性能http,https,websocket,tcp,udp,socks5

,ss代理服务器,支持正向代理、反向代理、透明代理、内网穿透、TCP/UDP端口映射、SSH中转

下载地址:

https://github.com/snail007/goproxy/

Transit server:

[crayon-5bf06608e9503966738693 inline="true" ]proxy socks -t tcp -p "0.0.0.0:8888" [/crayon]

Client使用代理工具连接Transit server的8888端口

4、自己使用go实现

Windows系统安装Go:

https://golang.org/dl/

安装git:

http://git-scm.com/downloads

安装go-socks5:

[crayon-5bf06608e9508436257838 inline="true" ]go get github.com/armon/go-socks5 go build [/crayon]

test.go:

[crayon-5bf06608e950c796874985 inline="true" ]package main import socks5 "github.com/armon/go-socks5" func main() {         conf := &socks5.Config{}         server, err := socks5.New(conf)         if err != nil {           panic(err)         }         if err := server.ListenAndServe("tcp", "0.0.0.0:8888"); err != nil {           panic(err)         } } [/crayon]

编译

[crayon-5bf06608e9512012116892 inline="true" ]go build test.go [/crayon]

Client使用代理工具连接Transit server的8888端口

5、使用reGeorg

下载地址:

https://github.com/NoneNotNull/reGeorg

针对web服务器,支持(aspx ashx jsp php)

注:

Windows下连接socks代理的工具可使用sockscap64

Linux下连接socks代理的工具可使用proxychains

0x05 反向代理0、应用场景1.内网扫描

对内网的多个端口进行扫描

测试环境如下图

Client无法正向连接到Transit server,但Transit server能够反向连接到Client

要对Server1、Server2和Server3的端口进行扫描

1、使用EarthWorm

Client:

[crayon-5bf06608e9517344528063 inline="true" ]ew -s rcsocks -l 2222 -e 1111 [/crayon]

Transit server:

[crayon-5bf06608e951c877445745 inline="true" ]ew -s rssocks -d 1.1.1.1 -e 1111 [/crayon]

使用代理工具连接Client的2222端口

2、使用rsocks

下载地址:

https://github.com/brimstone/rsocks

Go语言编写,支持Windows和Linux

Client:

[crayon-5bf06608e9521552604153 inline="true" ]rsocks -listen :1111 -socks 127.0.0.1:2222 [/crayon]

Transit server:

[crayon-5bf06608e9525427688202 inline="true" ]rsocks -connect 1.1.1.1:1111 [/crayon]

使用代理工具连接Client的2222端口

0x06 小结

本文对端口转发和代理的常用工具和方法进行整理总结,

划分正向和反向连接两个类别,分别介绍了应用场景和常用工具,可作为实际应用的参考。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3